package com.jxw.utils;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import net.lingala.zip4j.util.InternalZipConstants;
import org.apache.http.cookie.ClientCookie;

/* loaded from: classes.dex */
public class SDCardUtils {
    private static final String TYPE_CACHE = "cache";
    private static final String TYPE_FILES = "files";
    private static String sTfDir = "";

    public static boolean isMounted() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static String getExternalFileDir(Context context) {
        return getExternalDir(context, TYPE_FILES);
    }

    public static String getExternalCacheDir(Context context) {
        return getExternalDir(context, TYPE_CACHE);
    }

    private static String getExternalDir(Context context, String str) {
        File externalFilesDir;
        StringBuilder sb = new StringBuilder();
        if (context == null) {
            return null;
        }
        if (!isMounted()) {
            if (str.equals(TYPE_CACHE)) {
                sb.append(context.getCacheDir());
                sb.append(File.separator);
            } else {
                sb.append(context.getFilesDir());
                sb.append(File.separator);
            }
        }
        if (str.equals(TYPE_CACHE)) {
            externalFilesDir = context.getExternalCacheDir();
        } else {
            externalFilesDir = context.getExternalFilesDir(null);
        }
        if (externalFilesDir != null) {
            sb.append(externalFilesDir.getAbsolutePath());
            sb.append(File.separator);
        } else {
            sb.append(Environment.getExternalStorageDirectory().getPath());
            sb.append("/Android/data/");
            sb.append(context.getPackageName());
            sb.append(InternalZipConstants.ZIP_FILE_SEPARATOR);
            sb.append(str);
            sb.append(InternalZipConstants.ZIP_FILE_SEPARATOR);
            sb.append(File.separator);
            sb.toString();
        }
        return sb.toString();
    }

    public static String getInternalStorageDirectoryPath() {
        return Environment.getExternalStorageDirectory().getPath();
    }

    public static ArrayList<String> getExternalStorageDirectory() {
        String str;
        String[] split;
        String str2;
        new String();
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("mount").getInputStream()));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                } else if (!readLine.contains(ClientCookie.SECURE_ATTR) && !readLine.contains("asec")) {
                    if (readLine.contains("fat")) {
                        String[] split2 = readLine.split(" ");
                        if (split2 != null && split2.length > 1 && (str = split2[1]) != null && !arrayList.contains(str)) {
                            arrayList.add(str);
                            Logger logger = Logger.getLogger();
                            logger.d("1   " + str);
                        }
                    } else if (readLine.contains("fues") && (split = readLine.split(" ")) != null && split.length > 1 && (str2 = split[1]) != null && !arrayList.contains(str2)) {
                        arrayList.add(str2);
                        Logger logger2 = Logger.getLogger();
                        logger2.d("2   " + str2);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RuntimeException e2) {
            e2.printStackTrace();
        }
        return arrayList;
    }

    public static String getExternalStorageDirectoryPath(boolean z) {
        ArrayList<String> externalStorageDirectory = getExternalStorageDirectory();
        String str = null;
        if (externalStorageDirectory.size() <= 1) {
            return null;
        }
        String internalStorageDirectoryPath = getInternalStorageDirectoryPath();
        int indexOf = externalStorageDirectory.indexOf(internalStorageDirectoryPath);
        Logger logger = Logger.getLogger();
        logger.d("index: " + indexOf + internalStorageDirectoryPath);
        if (z) {
            if (!TextUtils.isEmpty(sTfDir)) {
                str = sTfDir;
            } else {
                int i = indexOf + 1;
                if (externalStorageDirectory.size() > i) {
                    str = externalStorageDirectory.get(i);
                }
            }
        } else if (externalStorageDirectory.size() > 2) {
            str = externalStorageDirectory.get(indexOf + 2);
        }
        Logger logger2 = Logger.getLogger();
        logger2.d("       " + str);
        return str;
    }

    public static String getExternalStorageDirectoryPath(Context context) {
        sTfDir = getTfStorageDirectory(context);
        return sTfDir;
    }

    private static String getTfStorageDirectory(Context context) {
        StorageManager storageManager = (StorageManager) context.getSystemService("storage");
        try {
            String[] strArr = (String[]) storageManager.getClass().getMethod("getVolumePaths", new Class[0]).invoke(storageManager, new Object[0]);
            if (strArr.length >= 2) {
                return strArr[1];
            }
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalArgumentException e2) {
            e2.printStackTrace();
            return null;
        } catch (NoSuchMethodException e3) {
            e3.printStackTrace();
            return null;
        } catch (InvocationTargetException e4) {
            e4.printStackTrace();
            return null;
        }
    }
}
